<?php

class ManageDoEditHyperlinkAction extends LtAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'tpl';
		$this->layout = 'bylayout';

		$this->data['headnavbar']['hyperlink'] = '';
		$this->data['headnavbar']['index'] = '';
		$this->data['headnavbar']['customer'] = '';
		$this->data['headnavbar']['visitlog'] = '';
	}

	public function execute()
	{
		$this->data['roleinfo'] = ManagerRole::getRoleInfFromCookie(C('LtUrl')->generate('manage','Hyperlink'));
		$linkid = '';
		$noticestr = '';
		$tmpdao = null;
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$postdata = $this->context->post('postdata');
			$linkid=$postdata['linkid'];
			if(!empty($linkid))
			{
				$thecat = '';
				if(isset($postdata['cat']))
				{
					foreach($postdata['cat'] as $key => $val)
					{
						$thecat .= "$key|";
					}
					if(!empty($thecat))
					{
						$thecat = "|$thecat";
					}
					unset($postdata['cat']);
					$postdata['cattags']=$thecat;
				}
				//echo $thecat;exit;

				$updata = $postdata;
				unset($updata['linkid']);
				$tmpdao = new HyperlinkDao();
				$upre = $tmpdao->editByid($linkid, $updata);
				if($upre)
				{
					$noticestr = "succ to do edit";
				}
				else
				{
					$noticestr = "fail to do edit";
				}
			}
		}
		else
		{
			$linkid = $this->context->get('linkid');
			$noticestr = $this->context->get('noticestr');
		}

		if(empty($linkid))
		{
			$this->doRedirect("fail to get linkid to load edit page");
		}
	
		$this->data['linkid'] = $linkid;
		$this->data['title'] = 'edit hyperlink';
		if(!empty($noticestr))
		{
			$noticestr = "<div class='warning'>{$noticestr}</div>";
		}
		$this->data['noticestr'] = $noticestr;

		$this->data['actionFilePath'] = __FILE__;
		
		if(empty($tmpdao))
		{
			$tmpdao = new HyperlinkDao();
		}
		$queryre = $tmpdao->getByid($linkid);//$queryre = array(0=>$queryre);
		if(empty($queryre))
		{	
			$this->doRedirect("fail to edit hyperlink with linkid=$linkid");
		}
		$this->template = "Manage-Edit-Hyperlink";
		$this->data['queryre'] = $queryre;
	
		$tmpvlog = new VisitlogDao();
		$queryre = $tmpvlog->getList("linkid=:linkid", array("linkid"=>$linkid), 'itime desc');
		//var_dump($queryre);exit;
		$this->data['vlog'] = $queryre;

		$this->data['headnavbar']['hyperlink'] = 'selected';
	}

	private function doRedirect($thenotice)
	{
		$noticestr = "<div class='warning'>{$thenotice}</div>";
		$redirectUrl = C('LtUrl')->generate('manage','Hyperlink', array('noticestr'=>$noticestr));
		header('Location: '.$redirectUrl);
	}
}
